#!/usr/local/bin/perl
#  9 Jan 1995 - added new AstroWeb categories
# 17 Jan 1995 - generate yellow pages from master database
# ------------------------------------------------------  AstroWeb @ NRAO 

$astrodir = '/vendor/webhead/WWW/docs/WWWVL/Astronomy/astroweb/';

@URL_file_list = ($astrodir, 
                  'http://fits.cv.nrao.edu/www/',
                  'astroweb.html') ;

&get_list(@URL_file_list);

# ----------------------------------------------------    AstroWeb @ STSCI
@URL_file_list = ($astrodir,
                 'http://www.stsci.edu/astroweb/',
                 'newmaster.html');

&get_list(@URL_file_list);

system ("cd  $astrodir");

$new_m  = "$astrodir/newmaster.html" ;
$old_m  = "$astrodir/newmaster.html.old" ;
$master = "$astrodir/master.html" ;

if  ( (-s "$new_m") !=  (-s "$old_m") ){
    system  ("tar -cf yp.tar yp*.html");
    print  "... Sorting $new_m \n" ;	
    system ("$astrodir/dtsort.pl sort=name < $new_m > $master ") ;

    print  "... Spliting $master \n" ;
    system ("$astrodir/dtsplit.pl style=vilspa < $master " );
    system ("/bin/chown sergio  $astrodir/*.html");
}
else { 
    print  "$new_m has not changed \n" ;
}

exit 0 ;

# =======================================================================
 
sub get_list {
  local($ldir,$URL_base,@file_list) = @_;
 
  $tmp = '/tmp/'; 
  ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,
  $mtime,$ctime,$blcksize,$bloks) = stat($ldir);

  foreach $file (@file_list){ 
    print "Getting $URL_base$file \n";
    if (system ("lynx  -source $URL_base$file > $tmp$file")== 0){
       chmod 0644 , "$tmp$file" ;
       chown $uid, $gid, "$tmp$file" ;
       open(F,"$tmp$file") ; 
       $line =  <F> ;
       if ( grep(/404 NOT FOUND/i,$line)  == 0){
           system("mv $ldir$file $ldir$file.old") if (-f "$ldir$file");
       	   if ( system("mv $tmp$file $ldir$file") == 0){
              $msg = "Mirroring $URL_base$file to $ldir$file ";
           }else{
              $msg = "*ERROR* Can't create $ldir$file ";
           }
        }else{
	      $msg = "*ERROR* Can't find $URL_base$file ";
        } 
    } else{
        $msg = "*ERROR* Can't access $URL_base$file ";
    }
    system("rm $tmp$file") if (-f "$tmp$file");
    print "$msg \n" ;        
  }	
}



